<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SfOrder;
use App\Models\OrderGoodsModel;
use App\Models\ShopModel;
use App\Models\ShopTypeModel;
use App\Models\StoreModel;
use App\Models\UserModel;
use App\Sevrice\OrderId;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Table;
use Illuminate\Http\Request;

class SfOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SfOrder(), function (Grid $grid) {
            $grid->column('order_id','序号')->sortable();
            $grid->column('order_sn');
            $grid->column('order_total');
            $grid->column('discount_value');
            $grid->column('order_status', '状态')->using(['1'=>'已付款','0'=>'待付款','2'=>'待服务','3'=>'待收货','4'=>'已完成','5'=>'已取消','6'=>'待评论']);
            $grid->column('delivery_name','收货人');
            $grid->column('delivery_tel','电话');
            $grid->column('delivery_address');
            $grid->column('order_settlement_time');
            $grid->column('order_amount_total');
            $grid->column('logistics_fee');
            $grid->column('pay_channel');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->export();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->column('content') ->display('详情') ->modal(function ($modal) {
                    // 设置弹窗标题
                    $modal->title('订单商品详情');
                    // 自定义图标
                    $modal->icon('feather icon-x');
                    $orderSn=$modal->row->order_sn;
                    $shopIds=OrderGoodsModel::where('order_ID',$orderSn)->pluck('goods_id');
                    $buyCount=OrderGoodsModel::where('order_ID',$orderSn)->pluck('goods_number');
                    $storeNum=OrderGoodsModel::where('order_ID',$orderSn)->pluck('store_num');
                    $one=StoreModel::whereIn('store_id',$storeNum)->pluck('store_name');
                    $data=ShopModel::whereIn('shop_id',$shopIds)->get(['shop_nick','shop_price'])->toArray();
                    foreach ($data as $k=>$v){
                        foreach ($buyCount as $key=>$val){
                            $data[$k]['goods_num']=$val;
                        }
                        foreach ($one as $k1=>$v1){
                            $data[$k]['store_name']=$v1;
                        }
                    }
                $titles = [
                    '商品名称',
                    '商品单价',
                    '购买数量',
                    '店铺'
                ];
                return Table::make($titles, $data);

                });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SfOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_sn');
            $show->field('user_num');
            $show->field('order_total');
            $show->field('discount_value');
            $show->field('order_status');
            $show->field('delivery_name');
            $show->field('delivery_tel');
            $show->field('delivery_address');
            $show->field('order_settlement_time');
            $show->field('order_amount_total');
            $show->field('logistics_fee');
            $show->field('pay_channel');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SfOrder(), function (Form $form) {
            $form->text('user_num', '用户手机号')->saving(function ($query) {
                return $userId = UserModel::where('user_tel', $query)->value('user_id');
            });
            $form->select('shop_type', '商品分类')->options('shopType')->load('shop_id', 'shopInfo');
            $form->select('shop_id', '商品编号')->options('shopInfo');
            $form->number('goods_num', '购买数量');
            $form->text('delivery_name');
            $form->text('delivery_tel');
            $form->text('delivery_address');
            $form->hidden('order_sn');
            $form->hidden('order_total');
            $form->hidden('order_amount_total');
            $form->hidden('created_at');
            $form->submitted(function (Form $form) {
                if ($form->isCreating()) {
                    $form->created_at= date('Y-m-d H:i:s');
                    $obj = new OrderId(1);
                    $data['order_sn'] = $obj->next();
                    $arr['order_ID'] = $data['order_sn'];
                    $arr['goods_id'] = substr($form->shop_id, 0, strpos($form->shop_id, '/'));
                    $arr['store_num'] = ShopModel::where('shop_id', $arr['goods_id'])->value('shop_name');
                    $arr['goods_price'] = substr($form->shop_id, strpos($form->shop_id, '/') + 1);
                    $arr['goods_number'] = $form->goods_num;
                    $data['order_total'] = $arr['goods_price'] * $arr['goods_number'];
                    $form->order_sn= $data['order_sn'];
                    $form->order_total= $data['order_total'];
                    $form->order_amount_total= $data['order_total'];
                    $arr['created_at'] = date('Y-m-d H:i:s');
                    OrderGoodsModel::insert($arr);
                }
                // 删除用户提交的数据
                $form->deleteInput('shop_id');
                $form->deleteInput('shop_type');
                $form->deleteInput('goods_num');
                // 中断后续逻辑
                if(!$data){
                    return $form->response()->error('服务器出错了~');
                }
            });
        });
    }

    function shopType()
    {
        $data = ShopTypeModel::get()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['id'] = $v['tid'];
            $data[$k]['text'] = $v['type_name'];
        }
        return $data;
    }

    function shopInfo(Request $request)
    {
        $shopType = $request->get('q');
        $data = ShopModel::where(function ($query) use ($shopType) {
            if ($shopType) {
                $query->where('shop_tid', $shopType);
            }
        })->get(['shop_id', 'shop_nick', 'shop_price'])->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['id'] = $v['shop_id'] . '/' . $v['shop_price'];
            $data[$k]['text'] = $v['shop_nick'] . '---/' . $v['shop_price'] . '元';
        }
        return $data;
    }

    function shopPrice(Request $request)
    {

    }
}
